<?php
/*
+--------------------------------------------------------------------------
|   Anwsion [#RELEASE_VERSION#]
|   ========================================
|   by Tatfook Network Team
|   (c) 2011 - 2012 Anwsion Software
|   http://www.anwsion.com
|   ========================================
|   Support: zhengqiang@gmail.com
|   
+---------------------------------------------------------------------------
*/

class c_maintain_class extends AWS_CONTROLLER
{

	function get_permission_action()
	{
	
	}

	public function setup()
	{
		admin_session_class::check_admin_login($this->get_permission_action());
		$this->crumb("系统维护", "admin/maintain/");
	}

	/**
	 * 默认action
	 * Enter description here ...
	 */
	public function index_action()
	{
	
	}

	/**
	 * 清除系统缓存
	 * Enter description here ...
	 */
	public function cache_action()
	{
		$this->crumb("缓存管理", "admin/maintain/");
		
		TPL::output('admin/maintain');
	}

	public function clean_cache_action()
	{
		$this->crumb("缓存管理", "admin/maintain/");
		
		$this->model('db_cache')->clean();
		
		TPL::assign('message', '成功清除网站全部缓存');
		TPL::output('admin/maintain_process');
	}

	public function keyword_index_action()
	{
		$this->crumb("更新关键字索引", "admin/maintain/keyword_index/");
		
		TPL::output('admin/keyword_index');
	}

	public function keyword_index_process_action()
	{
		$this->crumb("更新关键字索引", "admin/maintain/keyword_index/");
		
		if ($this->is_post())
		{
			$per_page = intval($_POST['per_page']);
			HTTP::redirect('/admin/maintain/keyword_index_process/per_page-' . $per_page);
		}
		
		$per_page = intval($_GET['per_page']);
		$page = intval($_GET['page']);
		$interval = intval($_GET['interval']);
		
		$question_num = $this->model('question')->get_question_list(true);
		
		if ($question_list = $this->model('question')->get_question_list(false, '', $page * $per_page . ',' . $per_page)) //未处理完，继续处理
		{
			$pa = new Services_Phpanalysis_Phpanalysis('utf-8', 'utf-8', false);
			//载入词典
			$pa->LoadDict();
			
			foreach ($question_list as $key => $val)
			{
				//执行分词
				$question_content_tmp = clear_point($val['question_content']);
				$pa->SetSource($question_content_tmp);
				$pa->StartAnalysis(true);
				$okresult = $pa->GetFinallyResult(',', false);
				$okresult_array = preg_split("/,/", $okresult);
				
				if (is_array($okresult_array))
				{
					$this->model('question')->delete_question_keyword($val['question_id']);
					
					$okresult_array = array_unique($okresult_array);
					foreach ($okresult_array as $py_key => $py_val)
					{
						$py_val = trim($py_val);
						{
							if ($py_val)
							{
								if (mb_strlen($py_val, 'utf-8') > 1)
								{
									$this->model('question')->add_question_keyword($val['question_id'], $py_val);
								}
							}
						}
					}
				
				}
			}
			
			++ $page;
			
			$current = ($page * $per_page) > $question_num ? $question_num : ($page * $per_page);
			
			$interval = ($interval == 0) ? 3 : $interval;
			
			$url = '/admin/maintain/keyword_index_process/page-' . $page . '__per_page-' . $per_page . '__interval-' . $interval;
			H::redirect_msg('总共' . $question_num . '个问题，当前更新到第' . $current . '个问题', $url, $interval);
		}
		else //提示处理完成
		{
			TPL::assign('msg', '关键词索引更新完成，总共更新' . $question_num . '个问题的关键词索引。');
			TPL::output('admin/keyword_index');
		}
	}

	public function database_action()
	{
		$this->crumb("数据库维护", "admin/maintain/database/");
		
		TPL::assign('backup_dir', $this->model('db_manage')->get_backup_dir());
		
		TPL::output('admin/database');
	}

	public function database_backup_action()
	{
		if ($this->is_post())
		{
			foreach ($_POST as $key => $val)
			{
				$gets[] = "$key-$val";
			}
			
			header('Location: ' . get_js_url('/admin/maintain/database_backup/' . implode('__', $gets)));
			exit();
		}
		
		$sqldump = '';
		
		$sizelimit = intval($_GET['sizelimit']);
		$volume = intval($_GET['volume']);
		$tableid = intval($_GET['tableid']);
		$startfrom = intval($_GET['startfrom']);
		
		$setnames = "SET NAMES 'UTF-8';\n\n";
		
		$tables = $this->model('db_manage')->get_table_list();
		
		if (! $tableid && $volume == 0)
		{
			foreach ($tables as $table)
			{
				$sqldump .= $this->model('db_manage')->dump_tbl_struct($table);
			}
		}
		
		$complete = TRUE;
		
		for (; $complete && $tableid < count($tables) && strlen($sqldump) + 500 < $sizelimit * 1000; $tableid ++)
		{
			$dump_query = $this->model('db_manage')->dump_tbl_record($tables[$tableid], $sizelimit, $startfrom, $complete, strlen($sqldump));
			
			$sqldump .= $dump_query['tabledump'];
			$startfrom = $dump_query['startfrom'];
			$complete = $dump_query['complete'];
				
			if ($complete)
			{
				$startfrom = 0;
			}
		}
		
		! $complete && $tableid --;
		
		if (trim($sqldump))
		{
			$volume ++;
			
			$retval = $this->model('db_manage')->save_sql_file($_GET['filename'], $sqldump, $volume);
			
			if ($retval)
			{
				//写入成功，继续进行
				H::redirect_msg('分卷备份：数据文件 #' . $volume . ' 创建成功，程序将自动继续...', '/admin/maintain/database_backup/sizelimit-' . rawurlencode($sizelimit) . '__volume-' . rawurlencode($volume) . '__tableid-' . rawurlencode($tableid) . '__startfrom-' . rawurlencode($startfrom) . '__filename-' . rawurlencode($_GET['filename']));
			}
			else
			{
				//写入出错
				die('无法保存文件，请检查system目录是否有写入权限');
			}
		}
		else
		{
			//提示备份完成，显示文件列表
			$this->crumb("数据库备份", "admin/maintain/database/");
			
			for($i = 1; $i <= $volume; $i ++)
			{
				$backup_list[] = 'system/backup_' . get_setting('backup_dir') . '/' . $_GET['filename'] . '-' . $i . '.sql';
			}
			
			TPL::assign('volume', $volume);
			
			TPL::assign('list', $backup_list);
			
			TPL::output('admin/database_backup_success');
		}
	}

	public function database_restore_action()
	{
	
	}

	public function sql_query_action()
	{
	
	}

}